package com.jackeysun.app_util

import android.content.Context
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.annotations.Expose
import com.jackeysun.util.*
import kotlinx.coroutines.*
import java.util.*
import kotlin.coroutines.Continuation
import kotlin.coroutines.coroutineContext
import kotlin.coroutines.suspendCoroutine

fun main() {

    val list = mutableListOf<Data>()

    for (i in 0..10) {
        list.add(Data("$i", i, Date()))
    }

    val data = Data("ssss", 19, Date())
//    println(list.toJson())
//    println(list.toJson().toObject<List<Data>>())

    val gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
    println(data.toJson {gson})
    println(data.toJson { gson }.toObject<Data> { gson })

}

data class Data(
    @Expose(deserialize = false) val string: String,
    val int: Int,
    val date: Date)

suspend fun fetchTwoDocs() = coroutineScope {
    println(Thread.currentThread().name)
    val deferreds = listOf(
        async { fetchOne(1) },
        async { fetchTwo(2) }
    )
    deferreds.awaitAll()
}

suspend fun fetchTwo(i: Int) {
    delay(2000 * i.toLong())
    println("fetchTwo")
}

suspend fun fetchOne(i: Int) {
    delay(2000)
    println("fetchOne")
}

suspend fun request() = suspendCancellableCoroutine<String> {
    switchThread{
        Thread.sleep(2000)
        it.resumeWith(result = Result.success("success"))
    }
}
